package com.mjt.admin.controller;


import com.mjt.admin.model.RespBean;
import com.mjt.admin.model.RespDataBean;
import com.mjt.admin.pojo.Customer;
import com.mjt.admin.query.CustomerQuery;
import com.mjt.admin.service.ICustomerService;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * <p>
 * 客户表 前端控制器
 * </p>
 *
 * @author 莫某人
 * @since 2021-11-01
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Resource
    private ICustomerService customerService;
    
    /**
     * @description 打开客户管理主页
     * @author 莫某人
     * @date 16:32 2021/11/1 
     * @return {@link String}
     **/
    @RequestMapping("index")
    public String index(){
        return "/customer/customer";
    }

    /**
     * @description 打开添加|更新客户信息
     * @author 莫某人
     * @date 16:32 2021/11/1
     * @return {@link String}
     **/
    @RequestMapping("addOrUpdateCustomerPage")
    public String addOrUpdateCustomerPage(Integer id, Model model){
        if (id==null){
            //添加页面
            return "/customer/add_update";
        }else {
            //更新页面
            model.addAttribute("customer",customerService.getById(id));
            return "/customer/add_update";
        }
    }



    /**
     * @description 获取客户列表
     * @author 莫某人
     * @date 16:53 2021/11/1
 * @param customerQuery
     * @return {@link RespDataBean< Customer>}
     **/


    @RequestMapping("/list")
    @ResponseBody
    public RespDataBean<Customer> customerList(CustomerQuery customerQuery){
        return customerService.customerList(customerQuery);
    }

    /**
     * @description 添加客户信息
     * @author 莫某人
     * @date 9:05 2021/11/2
 * @param customer
     * @return {@link RespBean}
     **/
    @RequestMapping("/save")
    @ResponseBody
    public RespBean saveCustomer(Customer customer){
        customerService.saveCustomer(customer);
        return RespBean.success("添加客户成功");
    }

    /**
     * @description 修改客户信息
     * @author 莫某人
     * @date 9:10 2021/11/2
 * @param customer
     * @return {@link RespBean}
     **/
    @RequestMapping("update")
    @ResponseBody
    public RespBean updateCustomer(Customer customer){
        customerService.updateCustomer(customer);
        return RespBean.success("修改客户信息成功");
    }

    /**
     * @description 删除客户信息
     * @author 莫某人
     * @date 9:11 2021/11/2
 * @param ids
     * @return {@link RespBean}
     **/

    @RequestMapping("delete")
    @ResponseBody
    public RespBean delete(Integer[] ids){
        customerService.deleteCustomer(ids);
        return RespBean.success("删除客户信息成功");
    }




}
